package com.association.main.controller;


import com.association.main.common.R;
import com.association.main.domain.Clubs;
import com.association.main.domain.Department;
import com.association.main.domain.JoinInfo;
import com.association.main.domain.Users;
import com.association.main.dto.ClubsDto;
import com.association.main.service.ClubsService;
import com.association.main.service.DepartmentService;
import com.association.main.service.JoinInfoService;
import com.association.main.service.UsersService;
import com.association.main.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/clubs")
public class ClubsController {

    @Autowired
    private ClubsService clubsService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private DepartmentService departmentService;


    @Autowired
    private JoinInfoService joinInfoService;

    @GetMapping
    public R<Page> clubsList(int page, int pageSize, Clubs clubs,Integer departmentId){
        //分页构造器
        log.info("{}",clubs);
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Clubs> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(clubs.getName() != null, Clubs::getName, clubs.getName());
        lambdaQueryWrapper.like(departmentId!=null,Clubs::getDepartmentId,departmentId);
        lambdaQueryWrapper.orderByDesc(Clubs::getCreatedAt);
        List<Clubs> clubList = clubsService.list(lambdaQueryWrapper);
        clubsService.page(pageInfo, lambdaQueryWrapper);


        List<ClubsDto> clubsDtos=clubList.stream().map((item)->{
            //遍历每一个clubslist
            ClubsDto clubsDto = new ClubsDto();
            BeanUtils.copyProperties(item,clubsDto);
            Users applyname = usersService.getById(item.getCreatedBy());
            Department department = departmentService.getById(item.getDepartmentId());
            LambdaQueryWrapper<JoinInfo> joinInfoServiceLambdaQueryWrapper=new LambdaQueryWrapper<>();
            joinInfoServiceLambdaQueryWrapper.eq(JoinInfo::getClubId, item.getId());
            int count = joinInfoService.count(joinInfoServiceLambdaQueryWrapper);
            clubsDto.setCount(count);
            clubsDto.setDepartment(department);
            clubsDto.setUsers(applyname);
            return clubsDto;
            //创建clubsDto
        }).collect(Collectors.toList());
        pageInfo.setRecords(clubsDtos);
        return R.success(pageInfo);
    }

    @GetMapping("/list")
    public R<List<Clubs>> clubsOption(HttpServletRequest request){
        String token = request.getHeader("token");
        String userType = JwtUtil.getUserType(token);
        LambdaQueryWrapper<Clubs> clubsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(userType.equals("member")){
            Integer userId = JwtUtil.getUserId(token);
            //如果是社团管理员，只显示管理的社团列表
            clubsLambdaQueryWrapper.eq(Clubs::getCreatedBy,userId);
        }

        List<Clubs> list = clubsService.list(clubsLambdaQueryWrapper);
        return R.success(list);
    }


    @PostMapping
    public R<Clubs> add(@RequestBody Clubs clubs){
        clubs.setUpdatedAt(LocalDateTime.now());
        boolean save = clubsService.save(clubs);
        return save == true ? R.success(clubs) : R.error("添加失败，未知错误");
    }

    @PutMapping
    public R<String> update(@RequestBody Clubs clubs){
        boolean update = clubsService.updateById(clubs);
        return update == true ? R.success("修改成功") : R.error("修改失败，未知错误");
    }

    @DeleteMapping
    public R<String> delete(@RequestBody Clubs clubs){
        boolean delete = clubsService.removeById(clubs.getId());
        return delete == true ? R.success("删除成功") : R.error("删除失败，未知错误");
    }
}
